﻿<?php
require_once('userDatabase.php');
require_once('playlistDatabase.php');
require_once('songDatabase.php');
/* 
 * Database.
 */
class Database {
	
	private $MYSQL_HOST = 'localhost';
	private $MYSQL_USER = 'root';
	private $MYSQL_PASS = '';
	private $MYSQL_DB = 'playnetdb';
	
	private $conexion=null;
	
	public $userDatabase = null;
	public $playlistDatabase = null;
	public $songDatabase = null;
	
	function __construct() {		
		$this->conexion = mysql_connect($this->MYSQL_HOST, $this->MYSQL_USER, $this->MYSQL_PASS);
		mysql_select_db($this->MYSQL_DB);
		mysql_query("SET NAMES 'utf8'");
		
		$this->userDatabase = new UserDatabase($this);
		$this->playlistDatabase = new PlayListDatabase($this);
		$this->songDatabase = new SongDatabase($this);
	}
	
	function __destruct() {
		if (is_resource($this->conexion) )
			mysql_close($this->conexion);
	}
	
	function sql($sql) {
		if (is_array($sql)) {
			foreach ($sql as $s) {
				$res = mysql_query($s);
				if (mysql_errno()) echo mysql_error().'<br>';
				$this->n++;
			}
		} else {
			$res = mysql_query($sql);
			if (mysql_errno()) echo mysql_error().'<br>';
			$this->n++;
		}
		return $res;
    }
	
	public function install() {
			
		// Borra las tablas si existen en la base de datos
		$sql = "DROP TABLE IF EXISTS `users`";
		$this->sql($sql);
		
		$sql = "DROP TABLE IF EXISTS `playlists`";
		$this->sql($sql);
			
		$sql = "DROP TABLE IF EXISTS `pl_rel_song`";
		$this->sql($sql);

		$sql = "DROP TABLE IF EXISTS `songs`";
		$this->sql($sql);
		
		$sql = "DROP TABLE IF EXISTS `votes_user_pl`";
		$this->sql($sql);
		
		$sql = "DROP TABLE IF EXISTS `favorites_user_pl`";
		$this->sql($sql);

		// Crea la tabla de usuarios
		$sql =" CREATE TABLE `users` (
				`id` INT PRIMARY KEY AUTO_INCREMENT,
				`name` VARCHAR(256),
				`password` VARCHAR(40),
				UNIQUE (`name`)
		)";
		$this->sql($sql);

		// Crea la tabla de play lists
		$sql =" CREATE TABLE `playlists` (
				`id` INT PRIMARY KEY AUTO_INCREMENT,
				`name` VARCHAR(256),
				`creatorId` INT,
				`positiveVotes` INT,
				`negativeVotes` INT
		)";
		$this->sql($sql);
			
		// Crea la tabla de canciones
		$sql =" CREATE TABLE `songs` (
				`id` INT PRIMARY KEY AUTO_INCREMENT,
				`title` VARCHAR(256),
				`link` VARCHAR(256),
				`player` INT,
				`idVideo` VARCHAR(256),
				`playlistCount` INT
		)";
		$this->sql($sql);
		
		// Crea la tabla que relaciona playlists con canciones
		$sql =" CREATE TABLE `pl_rel_song` (
				`playlistId` INT,
				`songId` INT,
				`order` INT NOT NULL,
				PRIMARY KEY (`playlistId`,`songId`)
		)";
		$this->sql($sql);	

		// Crea la tabla que relaciona los votos de usuarios con playlists
		$sql =" CREATE TABLE `votes_user_pl` (
				`userId` INT,
				`playlistId` INT,
				PRIMARY KEY (`userId`,`playlistId`)
		)";
		$this->sql($sql);
		
		// Crea la tabla que relaciona playlists favoritas con usuarios
		$sql =" CREATE TABLE `favorites_user_pl` (
				`userId` INT,
				`playlistId` INT,
				PRIMARY KEY (`userId`,`playlistId`)
		)";
		$this->sql($sql);
	}

}

?>